package cn.kgc.crowdfundingadmin.controller;

import cn.kgc.crowdfundingadmin.common.ResultEntity;
import cn.kgc.crowdfundingadmin.pojo.Cert;
import cn.kgc.crowdfundingadmin.service.CertService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shkstart
 * @create 郭祥伟
 */
@Controller
public class CertController {
    @Resource
    CertService certService;
    @ResponseBody
    @RequestMapping("/cert/search/by/keyword")
    public ResultEntity<PageInfo<Cert>> search(
            @RequestParam(value="pageNum", defaultValue="1",required = false) Integer pageNum,
            @RequestParam(value="pageSize", defaultValue="5",required = false) Integer pageSize,
            @RequestParam(value="keyword", defaultValue="",required = false) String keyword
    ) {

        // 1.查询得到PageInfo对象
        PageInfo<Cert> pageInfo = certService.queryForKeywordWithPage(pageNum, pageSize, keyword);

        // 2.封装结果对象返回
        return ResultEntity.successWithoutData(pageInfo);
    }


    @ResponseBody
    @RequestMapping("/cert/get/list/by/id/list")
    public ResultEntity<List<Cert>> getRoleListByIdList(@RequestBody List<Integer> certIdList) {

        List<Cert> roleList = certService.getCertListByIdList(certIdList);

        return ResultEntity.successWithoutData(roleList);
    }

    @ResponseBody
    @RequestMapping("/cert/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> certIdList) {

        certService.batchRemove(certIdList);

        return ResultEntity.successWithoutData();
    }


    @ResponseBody
    @RequestMapping("/cert/save/cert")
    public ResultEntity<String> saveCert(@RequestParam("certName") String certName) {

        certService.saveCert(certName);

        return ResultEntity.successWithoutData();
    }

    @ResponseBody
    @RequestMapping("/cert/update/cert")
    public ResultEntity<String> updateCert(Cert cert) {

        certService.updateCert(cert);

        return ResultEntity.successWithoutData();
    }
}
